package com.tnsw.noxnox.downloader;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class GCMManagerService {
	private static boolean sIsRunning = false;
	private static GCMManagerService sInstance;
	private String tag = "GCMManagerService";
	private ArrayList<GCMReceiverInterface> arrReceivers;
	
	public GCMManagerService() {
		
		this.arrReceivers = new ArrayList<GCMReceiverInterface>();
		Log.d(tag, "Consutructor()");
		
		sIsRunning = true;
		sInstance = this;
	}
	
	public static GCMManagerService getInstance() {
		if (sIsRunning == false) {
			sInstance = new GCMManagerService();
		} 
		
		return(sInstance);	
		
	}
	
	public void registerReceiver(GCMReceiverInterface receiver) {
		if (this.arrReceivers.contains(receiver) == false) {
			this.arrReceivers.add(receiver);
		}
	}
	
	public void unregisterReceiver(GCMReceiverInterface receiver) {
		this.arrReceivers.remove(receiver);
	}
	
	public void broadcast(Intent intent) {
		synchronized(this) {
			for (GCMReceiverInterface receiver : this.arrReceivers) {
				if (receiver != null) {
					receiver.onReceiveMessage(intent);
				}
			}
		}
	}	
}
